package com.csdig.cms.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.csdig.cms.common.ConstantDefine;
import com.csdig.cms.model.CmsSetting;
import com.csdig.cms.service.SettingService;
import com.csdig.cms.service.SpiderService;

import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class FrontUtils {
	/**
	 * 部署路径
	 */
	public static final String BASE = "base";
	/**
	 * 系统资源路径
	 */
	public static final String RES_SYS = "res";
	/**
	 * 系统资源路径
	 */
	public static final String REQ_URI = "req_uri";
	/**
	 * 请求路径
	 */
	public static final String REQ_PATH = "req_path";
	/**
	 * 系统资源路径
	 */
	public static final String URL_EXT = "url_ext";
	/**
	 * 系统资源路径
	 */
	public static final String CMS_SITE = "site";

	/**
	 * 系统资源路径
	 */
	public static final String SYS_DATE = "_sys_now";
	/**
	 * 起始条数
	 */
	public static final String FIRST = "first";
	/**
	 * 总条数
	 */
	public static final String COUNT = "count";

	/**
	 * 
	 * @param map
	 * @param uri
	 *            当前请求的uri
	 * @param urlExt
	 *            url后缀
	 * @throws Exception
	 */
	public static void frontGlobleData(Map<String, Object> map, String uri, String urlExt) throws Exception {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		String ctx = request.getContextPath();
		map.put(BASE, request);
		map.put(RES_SYS, ctx + ConstantDefine.FrontPage.ROOT_RES_DIR);
		SettingService settingSv = BeanFactory.getBeanByType(SettingService.class);
		List<CmsSetting> settings = settingSv.findAllFromCache();
		Map<String, String> setMap = new HashMap<String, String>();
		for (CmsSetting set : settings) {
			setMap.put(set.getName(), set.getValue());
		}
		String reuqestPath;
		int bi = uri.indexOf("_");
		int mi = uri.indexOf("-");
		int pi = uri.indexOf(".");
		if (bi != -1) {
			reuqestPath = uri.substring(0, bi);
		} else if (mi != -1) {
			reuqestPath = uri.substring(0, mi);
		} else if (pi != -1) {
			reuqestPath = uri.substring(0, pi);
		} else {
			reuqestPath = uri;
		}

		map.put(CMS_SITE, setMap);
		map.put(REQ_URI, uri);
		map.put(REQ_PATH, reuqestPath);
		map.put(URL_EXT, urlExt);
		map.put(SYS_DATE, new Date());
	}

	public static String pageNotFound(Map<String, Object> map) throws Exception {
		return ConstantDefine.FrontPage.TPL_BASE + "/" + "404" + ConstantDefine.FrontPage.TPL_SUFFIX;
	}

	/**
	 * 标签参数中获得条数。
	 * 
	 * @param params
	 * @return 如果不存在，或者小于等于0，或者大于5000则返回5000；否则返回条数。
	 * @throws TemplateException
	 */
	public static int getCount(Map<String, TemplateModel> params) throws TemplateException {
		Integer count = DirectiveUtils.getInt(COUNT, params);
		if (count == null || count <= 0 || count >= 5000) {
			return 5000;
		} else {
			return count;
		}
	}

	public static int getFirst(Map<String, TemplateModel> params) throws TemplateException {
		Integer first = DirectiveUtils.getInt(FIRST, params);
		if (first == null || first <= 0) {
			return 0;
		} else {
			return first - 1;
		}
	}

	public static String getDomain() throws Exception {
		SpiderService spider = BeanFactory.getBeanByType(SpiderService.class);
		return spider.getDomain();
	}

}
